<template>
  <div class="login df aic jcc">
    <div class="caontainer">
      <h3>系统登录</h3>
      <el-form :model="fromData" size="small" :rules="rules" ref="fromRef">
        <el-form-item prop="account">
          <!-- 用户 -->
          <el-input
            v-model="fromData.account"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <!-- 密码 -->
          <el-input
            v-model="fromData.password"
            :type="flag ? 'text' : 'password'"
            prefix-icon="el-icon-s-cooperation"
            @keydown.native.enter="submit"
          >
            <i
              slot="suffix"
              @click="flag = !flag"
              :class="flag ? 'el-icon-view' : 'el-icon-minus'"
            ></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapActions } from "vuex";
export default {
  data() {
    const checkPassword = (rules, value, callback) => {
      if (!value) {
        callback(new Error("请输入密码"));
      } else if (!/^\d{6,12}$/.test(value)) {
        callback(new Error("请输入6-12位密码"));
      } else {
        callback();
      }
    };
    return {
      fromData: {
        account: "",
        password: "",
      },
      flag: false,
      rules: {
        account: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 12, message: "长度在 3 到 12个字符", trigger: "blur" },
        ],
        password: [{ validator: checkPassword, trigger: "blur" }],
      },
    };
  },
  methods: {
    ...mapActions("user", ["A_user"]),
    submit() {
      this.$refs.fromRef.validate(async (valid) => {
        if (valid) {
          await this.A_user(this.fromData);
          this.$router.push("/home");
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background-color: $menuBg;
}
.caontainer {
  width: 300px;
  height: 300px;
  h3 {
    text-align: center;
    color: white;
    margin-bottom: 10px;
    font-weight: bold;
  }
  .btn {
    width: 100%;
  }
}
.el-input {
  width: 100%;
}

:deep(.el-input__inner) {
  background-color: transparent;
}
</style>